<?php

namespace App\Http\Controllers\Api;

use App\Models\goods;
use App\Models\goods_attr_val;
use App\Models\goods_image;
use App\Models\goods_sku;
use App\Models\order_info;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    public $pageSize = 30;
    /**
     * 商品详情
     */
    public function goodsDetail(Request $request)
    {
        try {
            $openid = $request->openid;
            $goods_id = $request->json('goods_id', 0);
            if(!$goods_id){
                fun_respon(0, '缺少参数！');
            }
            $goods_info = goods::where(['id'=>$goods_id])->first(['id as goods_id','title','retail_price','discount_price','original_price','goods_cover','goods_type','description','card_id','goods_detail_image']);
            if(!$goods_info){
                fun_respon(0, '系统繁忙，请稍后再试!');
            }
            $goods_info['discount_price'] = (float)$goods_info['discount_price'];
            $goods_info['retail_price'] = (float)$goods_info['retail_price'];

            //获取商品的sku
            $goods_sku = $this->getSku($goods_id);
            $goods_info['goods_sku'] = $goods_sku?$goods_sku:[];
            //获取banner图
            $banner_image = goods_image::where(['goods_id'=>$goods_id,'is_valid'=>1])->get();
            $goods_info['banner_image'] = $banner_image;
            //运费
            $goods_info['freight'] = get_freight();
            $goods_info['is_buy'] = $this->getIsBuy($goods_id,$openid);

            $goods_info['detail_image'] =[];
            if($goods_info['goods_detail_image']){
                $detail_image = goods_image::whereIn('id',explode(',',$goods_info['goods_detail_image']))->get(['image_url']);
                $goods_info['detail_image'] = $detail_image;
            }

            fun_respon(1, $goods_info);
        } catch (\Exception $ex) {
            fun_respon(0, $ex->getMessage().'系统繁忙，请稍后再试');
        }
    }

    /**
     * @param $goods_id
     * 获取商品的sku
     * @return goods_sku[]|array|\Illuminate\Database\Eloquent\Collection
     */
    private function getSku($goods_id){

        $order_goods = goods_sku::where(['goods_id'=>$goods_id])->get(['goods_sku.goods_id','sku_id','attr_symbol_path']);
        if($order_goods){
            foreach ($order_goods as &$value){
                $ids = explode(',',$value['attr_symbol_path']);
                $sku_name = goods_attr_val::getVal($ids);
                $value['sku_name'] = $sku_name?array_column($sku_name,'attr_value')[0]:'';
                $value['number'] = 1;
            }
            return $order_goods;
        }
        return [];
    }


    private function getIsBuy($goods_id,$user_id){
        $res = order_info::where(['user_id'=>$user_id,'order_status'=>2])->first();
        return $res?1:0;
    }



    /**
     * 商品列表
     */
    public function goodsList(Request $request)
    {
        try {
            $openid = $request->openid;
            $page = $request->json('page', 1);
            $offset = $this->pageSize*($page-1);
            $where = ['shelves'=>1];
            $data = goods::where($where)->take($this->pageSize)->skip($offset)->select('id','title','retail_price','discount_price','original_price','goods_cover','goods_type','description')->get();
            if($data){
                foreach ($data as &$v){
                    //查询订单的商品信息
                    //$v['goods_sku'] = $this->getSku($v['id']);
                    //查询是否购买过该商品
                    $v['is_buy'] = $this->getIsBuy($v['id'],$openid);
                }
            }

//            $type = $request->json('type', 1);
//            $goods_id = $request->json('goods_id', 176);
//            $goods_info = goods::where(['id'=>$goods_id])->first();
//            //var_dump($goods_info);exit;
//            $num = $type==1?1:50;
//            $current_price = $type==1?'19.8':'12.8';
//            $info['goods_id'] = 1;
//            $info['goods_name'] = '鹰嘴脆-全球首款鹰嘴脆零食';
//            $info['original_price'] = '29.8';
//            $info['current_price'] = $current_price;
//            $info['banner_image'] = [
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner0.png',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner1.jpg',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner2.jpg',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner3.jpg',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner4.jpg',
//            ];
//            $info['goods_sku'] = [
//                ['id'=>1,'name'=>'炭烧香辣味','number'=>$num],
//                ['id'=>2,'name'=>'芥末海苔味','number'=>$num],
//                ['id'=>3,'name'=>'枣夹核桃味','number'=>$num],
//                ['id'=>4,'name'=>'开心坚果味','number'=>$num],
//            ];
//            $info['detail_image'] = '';
            $datas['goods_list'] = $data;
            $datas['rate'] = 10;
            $datas['banner'] = [
                ['url'=>'https://recycle-1252100645.cos.ap-beijing.myqcloud.com/banner/WechatIMG818.jpeg','type'=>1],
                ['url'=>'https://recycle-1252100645.cos.ap-beijing.myqcloud.com/banner/WechatIMG1007.jpeg','type'=>1],
                ['url'=>'https://recycle-1252100645.cos.ap-beijing.myqcloud.com/banner/WechatIMG817.jpeg','type'=>0],
                ['url'=>'https://recycle-1252100645.cos.ap-beijing.myqcloud.com/banner/WechatIMG816.jpeg','type'=>0]
                ];
            fun_respon(1, $datas);
        } catch (\Exception $ex) {
            fun_respon(0, $ex->getMessage().'系统繁忙，请稍后再试');
        }
    }


}
